// pages/order/orderDetail/orderDetail.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: '',
    uid: '',
    cancel: ['收件人信息有误', '我不想买了', '拍错了', '重复下单', '其他原因'],
    show: false,
    orderid: '',
    xuid: '',
    sendshow: false,
    company: '',
    index: -1,
    inputValue1: '',
    inputValue2: '',
    inputValue3: ''
  },
  // 复制订单号
  copyText: function(e) {
    console.log(e)
    wx.setClipboardData({
      data: e.currentTarget.dataset.text,
      success: function(res) {
        wx.getClipboardData({
          success: function(res) {
            wx.showToast({
              title: '复制成功'
            })
          }
        })
      }
    })
  },
  //支付
  pay: function(e) {
    let that = this
    let id = e.currentTarget.dataset.id
    let price = e.currentTarget.dataset.price
    wx.request({

      url: 'https://bzl.isoft.mobi/index.php/Home/Pay/pay',
      // url: 'http://newbzl.didu86.com/index.php/Home/Pay/pay',
      // url: 'https://bzl.isoft.mobi/index.php/Home/Pay/pay',
      // url: 'https://bzl.isoft.mobi/index.php/Home/PayCeshi/pay',
      data: {
        uid: that.data.uid,
        oid: id,
        price: price
      },
      success: function(res) {
        console.log('支付', res)
        let json = res.data
        wx.requestPayment({
          appId: json.appId,
          timeStamp: json.timeStamp,
          nonceStr: json.nonceStr,
          package: json.package,
          signType: 'MD5',
          paySign: json.paySign,
          success: function(res) {
            console.log(res)
            wx.redirectTo({
              url: '/pages/buynow/buysuccess/buysuccess?show=' + 1 + '&price=' + price + '&id=' + id,
            })
          },
          fail: function(res) {
            console.log(res)
            wx.redirectTo({
              url: '/pages/buynow/buysuccess/buysuccess?show=' + 2 + '&price=' + price + '&id=' + id,
            })
          }
        })
      }
    })
  },
  // 提醒发货
  fahuo: function(e) {
    let id = e.currentTarget.dataset.id
    wx.request({
      url: app.globalData.api + "settixingfahuo",
      data: {
        uid: this.data.uid,
        id: id,
      },
      success: function(res) {
        console.log(res)

        wx.showToast({
          title: res.data.msg,
        })

      },
    })
  },
  //去发货框
  alertsend: function(e) {
    this.setData({
      sendshow: true,
    })
  },
  // 快递公司选择
  gongsi: function(e) {
    console.log(this.data.company[e.detail.value].name)
    this.setData({
      inputValue1: this.data.company[e.detail.value].name,
      index: e.detail.value
    })
  },
  tracking: function(e) {
    this.setData({
      inputValue2: e.detail.value
    })
    console.log(this.data.inputValue2)
  },
  beizhu: function(e) {
    this.setData({
      inputValue3: e.detail.value
    })
  },
  // 确认发货
  confirmSend: function(e) {
    let that = this
    if (!that.data.inputValue1 || !that.data.inputValue2) {
      wx.showToast({
        title: '请填写快递公司和单号',
      })
    } else {
      wx.request({
        url: app.globalData.api + "setfahuo",
        data: {
          id: that.data.detail.id,
          uid: that.data.xuid,
          name: that.data.inputValue1,
          number: that.data.inputValue2,
          text: JSON.stringify(that.data.inputValue3),
          type: 2
        },
        success: function(res) {
          console.log(res)
          if (res.data.code == 0) {
            wx.showToast({
              title: '发货成功',
            })
            that.setData({
              sendshow: false
            })
            wx.redirectTo({
              url: '/pages/person/shipments/shipments',
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        },
      })
    }

  },
  // 跳快递
  jumpexpress() {
    wx.navigateTo({
      url: '/pages/order/express/express',
    })
  },
  // 确认收货
  putgood: function(e) {
    let id = e.currentTarget.dataset.id
    wx.request({
      url: app.globalData.api + "setshouhuo",
      data: {
        id: id,
      },
      success: function(res) {
        console.log(res)
        if (res.data.code == 0) {
          wx.showToast({
            title: '收货成功',
          })
        }
      },
    })
  },
  // 跳评价
  jumpspeak: function(e) {
    let gid = e.currentTarget.dataset.gid
    let oid = e.currentTarget.dataset.oid
    let img = e.currentTarget.dataset.img
    console.log('id', gid, oid, img)
    wx.navigateTo({
      url: '/pages/speak/dospeak/dospeak?oid=' + oid + '&gid=' + gid + '&img=' + img,
    })
  },
  // 删除订单
  deleteorder: function(e) {
    let that = this
    wx.request({
      url: app.globalData.api + "getdelorder",
      data: {
        uid: that.data.uid,
        id: that.data.detail.id
      },
      success: function(res) {
        console.log('删除', res)
        if (res.data.code == 0) {
          wx.showToast({
            title: '删除成功',
          })
          setTimeout(() => {
            wx.redirectTo({
              url: '/pages/order/order',
            })
          }, 2000)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      },
    })
  },
  //取消订单
  cancelorder() {
    this.setData({
      show: true,
    })
  },
  // 取消
  cancel() {
    var that = this;
    that.setData({
      show: false,
      sendshow: false
    })
  },
  //选理由
  choose: function(e) {
    let text = e.currentTarget.dataset.text
    let index = e.currentTarget.dataset.index
    this.setData({
      note: text,
      piontindex: index
    })
  },
  // 确定取消订单
  confirm() {
    var that = this;
    wx.request({
      url: app.globalData.api + "getcancelorder",
      data: {
        uid: that.data.uid,
        id: that.data.orderid,
        note: that.data.note
      },
      success: function(res) {
        console.log('取消订单', res)
        if (res.data.code == 0) {
          that.setData({
            show: false
          })
          wx.showToast({
            title: '已取消',
          })
          wx.redirectTo({
            url: '/pages/order/order',
          })
        } else {
          wx.showToast({
            title: res.data.msg,
          })
        }
      },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this
    console.log(options)
    if (options.xuid) {
      // 公司选择接口
      wx.request({
        url: app.globalData.api + "getexpress",
        data: {
          uid: options.xuid,
        },
        success: function(res) {
          console.log('公司', res)
          that.setData({
            company: res.data.list
          })
          console.log(that.data.company)
        },
      })


      wx.request({
        url: app.globalData.api + "getorderdetail",
        data: {
          uid: options.xuid,
          id: options.id
        },
        success: function(res) {
          console.log('详情', res)
          that.setData({
            detail: res.data.list,
            orderid: res.data.list.id,
            xuid: options.xuid,

          })
        },
      })
    } else {
      wx.getStorage({
        key: 'userinfo',
        success: function(res) {
          that.setData({
            uid: res.data.id
          })
          wx.request({
            url: app.globalData.api + "getorderdetail",
            data: {
              uid: res.data.id,
              id: options.id
            },
            success: function(res) {
              console.log('详情', res)
              that.setData({
                detail: res.data.list,
                orderid: res.data.list.id
              })
            },
          })
        },
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})